package com.gengyun.gengyunacquistion.config;

import com.gengyun.gengyunacquistion.scheduled.DataAcquisition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.HashMap;
import java.util.Map;


public class QuartzConfig2 {


    /**
     * 创建 job 对象
     * @return
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 关联自己的job类
        factoryBean.setJobClass(DataAcquisition.class);
        factoryBean.setName("job1");
        Map<String, String> map = new HashMap<>();
        map.put("concurrent", "false");
        factoryBean.setJobDataAsMap(map);
        return factoryBean;
    }

//    @Bean
//    public JobDetailFactoryBean jobDetailFactoryDailyBean() {
//        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
//        // 关联自己的job类
//        factoryBean.setJobClass(DataAcquisitionDaily.class);
//        factoryBean.setName("job2");
//        return factoryBean;
//    }


    /**
     * 创建 Trigger 对象
     * @return
     */
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        // 关联jobDetail对象
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        // 设置时间
        cronTriggerFactoryBean.setCronExpression("1/5 * * * * ?");
        cronTriggerFactoryBean.setName("trigger1");
        Map<String, String> map = new HashMap<>();
        map.put("concurrent", "false");
        cronTriggerFactoryBean.setJobDataAsMap(map);
        return cronTriggerFactoryBean;
    }
//
//    @Bean
//    public CronTriggerFactoryBean cronTriggerFactoryDailyBean(JobDetailFactoryBean jobDetailFactoryDailyBean) {
//        CronTriggerFactoryBean cronTriggerFactoryDailyBean = new CronTriggerFactoryBean();
//        // 关联jobDetail对象
//        cronTriggerFactoryDailyBean.setJobDetail(jobDetailFactoryDailyBean.getObject());
//        // 设置时间
//        cronTriggerFactoryDailyBean.setCronExpression("1/5 * * * * ?");  //0 0 6 * * ?
//        cronTriggerFactoryDailyBean.setName("trigger2");
//        return cronTriggerFactoryDailyBean;
//    }


    /**
     * 创建 Schedule 对象
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        // 关联Trigger对象
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        // 配置注入
        schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
        return schedulerFactoryBean;
    }
}
